package clientInfo

import (
	"LRAdminServer/global"
	"LRAdminServer/model/clientInfo"
	"LRAdminServer/model/common/request"
    clientInfoReq "LRAdminServer/model/clientInfo/request"
)

type ClientPlatformService struct {
}

// CreateClientPlatform 创建登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService) CreateClientPlatform(client *clientInfo.ClientPlatform) (err error) {
	err = global.GVA_DB.Create(client).Error
	return err
}

// DeleteClientPlatform 删除登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService)DeleteClientPlatform(client clientInfo.ClientPlatform) (err error) {
	err = global.GVA_DB.Delete(&client).Error
	return err
}

// DeleteClientPlatformByIds 批量删除登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService)DeleteClientPlatformByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]clientInfo.ClientPlatform{},"id in ?",ids.Ids).Error
	return err
}

// UpdateClientPlatform 更新登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService)UpdateClientPlatform(client clientInfo.ClientPlatform) (err error) {
	err = global.GVA_DB.Save(&client).Error
	return err
}

// GetClientPlatform 根据id获取登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService)GetClientPlatform(id uint) (client clientInfo.ClientPlatform, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&client).Error
	return
}

// GetClientPlatformInfoList 分页获取登录平台记录
// Author [piexlmax](https://github.com/piexlmax)
func (clientService *ClientPlatformService)GetClientPlatformInfoList(info clientInfoReq.ClientPlatformSearch) (list []clientInfo.ClientPlatform, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&clientInfo.ClientPlatform{})
    var clients []clientInfo.ClientPlatform
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }
	
	err = db.Find(&clients).Error
	return  clients, total, err
}
